package com.xw.controller;

import com.xw.mapper.Mymapper;
import com.xw.pojo.JwtHelper;
import com.xw.pojo.Result;
import com.xw.pojo.User02;
import com.xw.service.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class Controller02 {
    @Autowired
    private Mymapper mymapper;

    @Autowired
    private Service service;

    @Autowired
    private JwtHelper jwtHelper;


    @GetMapping("R")
    public Result R() {
        Object user02s = mymapper.find();

        if (user02s != null) {
            Result rs = Result.ok();
            rs.setData(user02s);
            return rs;
        }
        return  null;
    }

//    ----------------------------------------
    @GetMapping("A")
    public  Result A(String id){
        List<User02> user02s = mymapper.findbyid(id);


        //这里需要isempy判断,因为sql没有查询到返回空集合【】
      // 而查询失败是没有集合null
        if(user02s.isEmpty() || user02s==null){
            Result error = Result.error();
            return error;
        }
        String token = jwtHelper.createToken(7999L);
        System.out.println("生成的token码为：" + token);
        Result rs = Result.ok();
        rs.setJwttoken(token);
        rs.setData(user02s);
        return  rs;
    }




//    String token = jwtHelper.createToken(7L);
//        System.out.println("生成的token码为：" + token);
//
//    //解析用户标识
//    int userId = jwtHelper.getUserId(token).intValue();
//        System.out.println("解密token码用户userId：" + userId);
//
//    //校验是否到期! false 未到期 true到期
//    boolean expiration = jwtHelper.isExpiration(token);
//        System.out.println("是否过期：" + expiration);

}



